﻿using System.Linq;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Model.Setting;
using Labthink.PCSoft.UniversalHost4.Utility;
using Newtonsoft.Json;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcRemote
    {
        private NotifyManager _notify;
        public UcRemote()
        {
            InitializeComponent();
            BtnOk.Focus();
        }

        public string Title { get; set; } = LanguageManager.GetLanguageValue("SystemSet-Remote");

        public string PermissionCode { get; set; } = "Per_RemoteSetting";

        private async void btnDSTest_Click(object sender, RoutedEventArgs e)
        {
            _triggerButtonsStatus(false);
            if (string.IsNullOrEmpty(TbRtHost.Value)|| string.IsNullOrEmpty(TbRtShare.Value)||string.IsNullOrEmpty(TbRtUser.Value)|| string.IsNullOrEmpty(TbRtPassword.Value)) return;
            var ri = new RemoteInfo
            {
                Host = TbRtHost.Value.Trim(),
                Share = TbRtShare.Value.Trim(),
                User = TbRtUser.Value.Trim(),
                Password = TbRtPassword.Value.Trim()
            };
            var sd = await Common.Instrument.Commands.TestRemoteHostAsync(ri);
            if (sd.ErrorCode < 0)
            {
                //MessageBox.Show(sd.ErrorMsg);
                _notify.ShowError(sd.ErrorMsg);
            }
            else
            {
                _notify.ShowSuccess(sd.ErrorMsg);
            }

            _triggerButtonsStatus(true);
        }

        private async void btnOk_Click(object sender, RoutedEventArgs e)
        {
            _triggerButtonsStatus(false);
            if (string.IsNullOrEmpty(TbRtHost.Value) || string.IsNullOrEmpty(TbRtShare.Value) || string.IsNullOrEmpty(TbRtUser.Value) || string.IsNullOrEmpty(TbRtPassword.Value)) return;
            var ri = new RemoteInfo
            {
                Host = TbRtHost.Value.Trim(),
                Share = TbRtShare.Value.Trim(),
                User = TbRtUser.Value.Trim(),
                Password = TbRtPassword.Value.Trim()
            };
            var sd = await Common.Instrument.Commands.SetRemoteHostAsync(ri);
            if (sd.ErrorCode < 0)
            {
                //MessageBox.Show(sd.ErrorMsg);
                _notify.ShowError(sd.ErrorMsg);
            }
            else
            {
                _notify.ShowSuccess(sd.ErrorMsg);
            }

            _triggerButtonsStatus(true);
        }

        private async void UcRemote_OnLoaded(object sender, RoutedEventArgs e)
        {
            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);
            _triggerButtonsStatus(false);
            var sd = await Common.Instrument.Commands.GetRemoteHostAsync();
            if (sd.ErrorCode < 0)
            {
                //MessageBox.Show(sd.ErrorMsg);
                _notify.ShowError(sd.ErrorMsg);
            }
            else
            {
                var r = sd.Value;
                if (!string.IsNullOrEmpty(r))
                {
                    var ri = JsonConvert.DeserializeObject<RemoteInfo>(r);
                    TbRtHost.Value = ri.Host;
                    TbRtShare.Value = ri.Share;
                    TbRtUser.Value = ri.User;
                    TbRtPassword.Value = ri.Password;
                }

            }

            _triggerButtonsStatus(true);
        }

        private void _triggerButtonsStatus(bool status)
        {
            BtnOk.IsEnabled = status;
            BtnDsTest.IsEnabled = status;
            Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}
